package com.rytong.tools.ui;

import java.util.Calendar;
import java.util.TimeZone;
import com.rytong.tools.R;
import com.rytong.tools.utils.EventHandler;
import com.rytong.tools.utils.Utils;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.text.InputFilter;
import android.text.method.DateKeyListener;
import android.text.method.DialerKeyListener;
import android.text.method.DigitsKeyListener;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class LPTextField extends Component {
	// input constraint.(only for TextField)
	private static final String ATTRIBUTE_INPUT_CONSTRAINT = "constraint";
	// input max length.(only for TextField)
	private static final String ATTRIBUTE_INPUT_MAXLENG = "maxleng";
	// input min length.(only for TextField)
	private static final String ATTRIBUTE_INPUT_MINLENG = "minleng";
	//判断是否添加联系人
	private boolean islinkman_ = false ;
	private Bitmap addButton = null ;
	BaseView  bv_ ;
	// 输入框限制输入类型
	final static int InputType_TYPE_CLASS_TEXT = 0;
	final static int InputType_TYPE_TEXT_VARIATION_PASSWORD = InputType_TYPE_CLASS_TEXT + 1;
	final static int InputType_TYPE_NUMBER_FLAG_DECIMAL = InputType_TYPE_TEXT_VARIATION_PASSWORD + 1;
	final static int InputType_TYPE_CLASS_NUMBER = InputType_TYPE_NUMBER_FLAG_DECIMAL + 1;
	final static int InputType_TYPE_CLASS_DATETIME = InputType_TYPE_CLASS_NUMBER + 1;
	final static int InputType_TYPE_CLASS_PHONE = InputType_TYPE_CLASS_DATETIME + 1;
	final static int InputType_TYPE_TEXT_VARIATION_VISIBLE_PASSWORD = InputType_TYPE_CLASS_PHONE + 1;

	private static final int BORDER_INDENT = 2;

	/**
	 * Temporary string for inputing.
	 */
	private String temp_;

	int inputFieldWidth_;

	private String isNotNull;

	public String getIsNotNull() {
		return isNotNull == null ? "" : isNotNull;
	}

	public void setIsNotNull(String isNotNull) {
		this.isNotNull = isNotNull;
	}

	private String alertStr;

	public String getAlertStr() {
		return alertStr;
	}

	public void setAlertStr(String alertStr) {
		this.alertStr = alertStr;
	}

	public class MyLPTextField extends EditText implements CompositedComponent {

		//private BaseView activity_;
		public boolean isDateField_;
		private boolean isWindowVisiable_;
		//add by zhaobin 2011.1.5 start 加入扫描按钮
		public boolean addScanButton=false;
		private int padLeft = -1;
		public boolean isNeedDecode=false;

		//买基金 新增搜索按钮
		public boolean issearch = false ;
		private int searchpadleft = -1 ;
		public String searchurl ;
		public String searchbody ;
		
		private InputMethodManager inputManager_ = null;
		int maxSize;
		//分离显示和提交的文本
		private String subValue;

		public String getSubValue() {
			return subValue;
		}
		public void setSubValue(String subValue) {
			this.subValue = subValue;
		}

		public MyLPTextField(Context context, String value) {
			super(context);
			bv_=(BaseView)context;
			
			inputManager_ = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
			maxSize = maxLeng();
			bv_.maxSize = this.maxSize;
			if (maxSize > 0) {
				setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxSize) });
			}
			setFocusable(true);
			setFocusableInTouchMode(true);
			// set content.
			setText(getPropertyByName("value").toString());

			String subValue=getPropertyByName("subvalue");
			setSubValue(subValue);

			// set constraint.
			setConstraint(getPropertyByName("style"));
			// 产品规定的输入框为单行输入效�?
			setSingleLine();
			//判断是否有添加联系人(号码)
			String LinkManstr = (String)getPropertyByName("LinkMan");
			if(LinkManstr.equalsIgnoreCase("LinkMan")&&!"".equals(LinkManstr)){
				islinkman();
			}
			//判断是否在添加联系人，选择号码后将联系人姓名赋值给该文本
			String islkname = (String)getPropertyByName("islkname");
			if(islkname.equalsIgnoreCase("true")&&!"".equals(islkname)){
				bv_.setlknameLPText(this);
			}
			// 扫描2维码
			String isScan = (String) getPropertyByName("scan");
			if (isScan.equals("true")) {
				String needDecode=(String)getPropertyByName("decode");
				if(needDecode.equalsIgnoreCase("true")&&!"".equals(needDecode)){
					isNeedDecode=true;
				}
				drawScanIcon();
			}
			String alertStr=(String)getPropertyByName("alertName");
			setAlertStr(alertStr);

			//买基金增加搜索按钮
			String isbeat = (String) getPropertyByName("search");
			if(isbeat.equals("true")){
				issearch = true ;
				searchurl = (String) getPropertyByName("searchurl");
				searchbody = (String) getPropertyByName("searchbody");
				drawSearchIcon() ;
			}
		}
		
		//增加联系人
		public void islinkman(){
			islinkman_ = true ;
			if(addButton == null || addButton.isRecycled()){
				addButton = ((BitmapDrawable) this.getResources().getDrawable(R.drawable.addbutton)).getBitmap();
				//addButton = Bitmap.createScaledBitmap(addButton, Utils.getScaledValueX(addButton.getWidth()-10), Utils.getScaledValueX(addButton.getHeight()-10), true);        		
			}
			this.setCompoundDrawablesWithIntrinsicBounds(null, null, new BitmapDrawable(addButton), null);
		}

		/**
		 * @param constraints
		 */
		void setConstraint(Object constraint) {
			if (null != constraint) {
				String format = (String) constraint;
				int j = format.indexOf(";");
				if (j >= 0) {
					String front = format.substring(0, j).trim();
					String last = format.substring(j + 1).trim();
					if (front.indexOf("-wap-input-format") != -1) {
						if (front.indexOf("'n'") != -1) {
							setKeyListener(new DigitsKeyListener(false, true));
						} else if (front.indexOf("'N'") != -1) {
							setKeyListener(new DigitsKeyListener(true, false));
						} else if (front.indexOf("'date'") != -1) {
							setDateStyle();
							setKeyListener(new DateKeyListener());
						} else if (front.indexOf("'phone'") != -1) {
							setKeyListener(new DialerKeyListener());
						}
					} else if (last.indexOf("-wap-input-format") != -1) {
						if (last.indexOf("'n'") != -1) {
							setKeyListener(new DigitsKeyListener(false, true));
						} else if (last.indexOf("'N'") != -1) {
							setKeyListener(new DigitsKeyListener(true, false));
						} else if (last.indexOf("'date'") != -1) {
							setDateStyle();
							setKeyListener(new DateKeyListener());
						} else if (last.indexOf("'phone'") != -1) {
							setKeyListener(new DialerKeyListener());
						}
					}
					if (front.indexOf("-wap-input-required") != -1) {
						if (front.indexOf("'true'") != -1) {
							setIsNotNull("true");
						}
					} else if (last.indexOf("-wap-input-required") != -1) {
						if (last.indexOf("'true'") != -1) {
							setIsNotNull("true");
						}
					}
				} else {
					if (format.indexOf("-wap-input-format") != -1) {
						if (format.indexOf("'n'") != -1) {
							setKeyListener(new DigitsKeyListener(false, true));
						} else if (format.indexOf("'N'") != -1) {
							setKeyListener(new DigitsKeyListener(true, false));
						} else if (format.indexOf("'date'") != -1) {
							setDateStyle();
							setKeyListener(new DateKeyListener());
						} else if (format.indexOf("'phone'") != -1) {
							setKeyListener(new DialerKeyListener());
						}
					} else if (format.indexOf("-wap-input-required") != -1) {
						if (format.indexOf("'true'") != -1) {
							setIsNotNull("true");
						}
					}
				}
			}
		}

		public boolean isAddPhoneButton() {
			return (islinkman_);
		}
		
		@Override
		public boolean dispatchTouchEvent(MotionEvent event) {
			float curX = event.getX();
			String isKeyBoard = (String)getPropertyByName("isKeyBoard");
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				if (isKeyBoard != null && isKeyBoard.equals("true")) {
					this.requestFocus();
					inputManager_.hideSoftInputFromWindow(this.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
					if(Utils.keyDlg_ == null || !Utils.keyDlg_.isShowing()){
						Utils.showInputWindow(bv_, this, Utils.sc_);						
					}
					return true;
				}else {
					if (isDateField_) {
						// 预防控件出现连击效果
						long currentTime = System.currentTimeMillis();
						long timedifference = currentTime - LASTCLICKTIME;
						if (timedifference < 0) {
							LASTCLICKTIME = currentTime;
						} else if (timedifference < jetLag_) {
							LASTCLICKTIME = currentTime;
							return false;
						} else {
							LASTCLICKTIME = currentTime;
						}

						onCreateSelectDialog(bv_);
					}
					// 增加联系人
					if (islinkman_) {
						if (curX > (getWidth() - addButton.getWidth())) {
							// MyLPTextField Temp = (MyLPTextField) realView_;
							bv_.setPhoneLPTextField(this);
							bv_.getPeopleConnects(bv_);
							InputMethodManager iim = (InputMethodManager) bv_.getSystemService(Context.INPUT_METHOD_SERVICE);
							if (iim.isActive() && bv_.getCurrentFocus() != null) {
								iim.hideSoftInputFromWindow(bv_.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
							}
							return true;
						} else {
							return super.onTouchEvent(event);
						}
					}

					//MOD BY ZHAOBIN 2012.1.5
					if (addScanButton) {
						if (curX > this.getWidth()-padLeft) {
							EventHandler uh = EventHandler.getUtHandler();
							Message msg = new Message();
							msg.what = EventHandler.MSG_QRCORD_SCAN_SHOW_TEXT;
							msg.obj = LPTextField.MyLPTextField.this;
							uh.eventHandler.sendMessage(msg);
							return true;
						} else {
							return super.onTouchEvent(event);
						}
					}
					//MOD BY ZHAOBIN 2012.1.5
					yDown_ = event.getY();
					
					//增加基金search搜索按钮
					if(issearch){
						if(curX > this.getWidth() - searchpadleft){
							EventHandler uh2 = EventHandler.getUtHandler() ;
							Bundle bundle = new Bundle();
							searchurl = Utils.SERVER_URI + "/" + searchurl ;
							Utils.printOutToConsole("searchurl-----" + searchurl );
							bundle.putString("searchurl", searchurl);
							searchbody = searchbody+"&query="+this.getText();
							bundle.putString("searchbody", searchbody);
							Message ms = new Message();
							ms.what = EventHandler.MSG_SEARCH_JIJIN ;
							ms.setData(bundle) ;
							uh2.eventHandler.sendMessage(ms);
						}
					}
				}
				break;
			case MotionEvent.ACTION_MOVE:
				moveY(yDown_, event.getY());
				break;
			}
			return super.dispatchTouchEvent(event);
		}

		void setDateStyle() {
			isDateField_ = true;
			this.setBackgroundResource(0);
			setFocusable(false);
			String valueAttr = (String) getPropertyByName("value");
			valueAttr = valueAttr.trim();
			if (valueAttr.length() > 0) {
				// value属性有�?需要按照showFormat的格式将value值进行显�?如果不给showFormat属性显示默认带"-")
				String showFormat = (String) getPropertyByName("showFormat");
				StringBuffer buffer = new StringBuffer();
				Calendar cal = Calendar.getInstance();
				cal.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 设置时区。
				if (valueAttr.contains("-")) {
					// value值存�?-". eg�?012-12-12
					if (!showFormat.equals("") && !showFormat.contains("-")) {
						// showFormat属性为eg:yyyyddmm �??��示的日期需要去"-";反之直接将value值进行显�?
						valueAttr = valueAttr.replace("-", "");
						// 不含横线。
						cal.set(Calendar.YEAR, Integer.parseInt(valueAttr.substring(0, 4))); // 设置年份。
						cal.set(Calendar.MONTH, Integer.parseInt(valueAttr.substring(4, 6)) - 1); // 设置月份。
						cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(valueAttr.substring(6, 8))); // 设置日期。
					} else {
						// 含有横线。
						cal.set(Calendar.YEAR, Integer.parseInt(valueAttr.substring(0, 4))); // 设置年份。
						cal.set(Calendar.MONTH, Integer.parseInt(valueAttr.substring(5, 7)) - 1); // 设置月份。
						cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(valueAttr.substring(8, 10))); // 设置日期。
					}
				} else {
					// value值为如下格式�?eg:20121212
					if ((showFormat.equals("") || showFormat.contains("-")) && valueAttr.length() == 8) {
						// showFormat属性为如下格式时eg:yyyy-mm-dd时，需要给value值转化成该格式进行显�?
						buffer.append(valueAttr.substring(0, 4)).append("-").append(valueAttr.substring(4, 6))
						.append("-").append(valueAttr.substring(6));
						valueAttr = buffer.toString();
						// 含有横线。
						cal.set(Calendar.YEAR, Integer.parseInt(valueAttr.substring(0, 4))); // 设置年份。
						cal.set(Calendar.MONTH, Integer.parseInt(valueAttr.substring(5, 7)) - 1); // 设置月份。
						cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(valueAttr.substring(8, 10))); // 设置日期。
					} else {
						// 不含横线。
						cal.set(Calendar.YEAR, Integer.parseInt(valueAttr.substring(0, 4))); // 设置年份。
						cal.set(Calendar.MONTH, Integer.parseInt(valueAttr.substring(4, 6)) - 1); // 设置月份。
						cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(valueAttr.substring(6, 8))); // 设置日期。
					}
				}
				// 如果格式字符串中含有EEEE字段，表示需要拼接上星期几。
				if(showFormat != null && showFormat.contains("EEEE")){
					valueAttr += " " + Utils.getDayOfWeek(bv_, cal);
				}
				setText(valueAttr);
			} else {
				setText(createCurrentDateString());
			}
			setDefaultValue(getPropertyByName("value"), getPropertyByName("valueFormat"));
		}

		private String createCurrentDateString() {
			final Calendar calendar = Calendar.getInstance();
			calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
			int year = calendar.get(Calendar.YEAR);
			int month = calendar.get(Calendar.MONTH) + 1;
			int day = calendar.get(Calendar.DAY_OF_MONTH);
			String dateStr = year + "" + (month < 10 ? ("0" + month) : month) + "" + (day < 10 ? ("0" + day) : day);
			String showFormat = (String) getPropertyByName("showFormat");
			if(showFormat != null && showFormat.contains("EEEE")){ // 需要拼接星期几的信息。
				dateStr += " " + Utils.getDayOfWeek(bv_, calendar);
			}
			return dateStr;
		}

		/**
		 * 给value属性赋默认值。
		 * 
		 * @param defaultValue 给定的默认值。
		 * @param valueFormat 格式化字符串。
		 */
		private void setDefaultValue(String defaultValue, String valueFormat){
			valueFormat = (valueFormat == null) ? "yyyyMMdd" : valueFormat;
			// 后台给定了value属性。
			if (defaultValue != null && defaultValue.trim().length() > 0) {
				// value属性需要根据valueFormat格式字符串来进行格式化。
				StringBuffer buffer = new StringBuffer();
				Calendar cal = Calendar.getInstance();
				cal.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 设置时区。
				if (defaultValue.contains("-")) { // 给定的值中含有-。
					if (!valueFormat.equals("") && !valueFormat.contains("-")) { // 格式字符串中不含-。
						defaultValue = defaultValue.replace("-", "");
						cal.set(Calendar.YEAR, Integer.parseInt(defaultValue.substring(0, 4))); // 设置年份。
						cal.set(Calendar.MONTH, Integer.parseInt(defaultValue.substring(4, 6)) - 1); // 设置月份。
						cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(defaultValue.substring(6, 8))); // 设置日期。
					} else { // 格式字符串中含有-。
						cal.set(Calendar.YEAR, Integer.parseInt(defaultValue.substring(0, 4))); // 设置年份。
						cal.set(Calendar.MONTH, Integer.parseInt(defaultValue.substring(5, 7)) - 1); // 设置月份。
						cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(defaultValue.substring(8, 10))); // 设置日期。
					}
				} else { // 给定的值中不含-。
					if ((valueFormat.equals("") || valueFormat.contains("-")) && defaultValue.length() == 8) { // 格式字符串含有-。
						buffer.append(defaultValue.substring(0, 4)).append("-").append(defaultValue.substring(4, 6))
						.append("-").append(defaultValue.substring(6));
						defaultValue = buffer.toString();
						cal.set(Calendar.YEAR, Integer.parseInt(defaultValue.substring(0, 4))); // 设置年份。
						cal.set(Calendar.MONTH, Integer.parseInt(defaultValue.substring(5, 7)) - 1); // 设置月份。
						cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(defaultValue.substring(8, 10))); // 设置日期。
					} else { // 格式字符串不含-。
						cal.set(Calendar.YEAR, Integer.parseInt(defaultValue.substring(0, 4))); // 设置年份。
						cal.set(Calendar.MONTH, Integer.parseInt(defaultValue.substring(4, 6)) - 1); // 设置月份。
						cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(defaultValue.substring(6, 8))); // 设置日期。
					}
				}
				// 如果格式字符串中含有EEEE字段，表示需要拼接上星期几。
				if(valueFormat != null && valueFormat.contains("EEEE")){
					defaultValue += " " + Utils.getDayOfWeek(bv_, cal);
				}
				setPropertyByName("value", defaultValue);
			} 
			// 后台没给value属性。
			else {
				final Calendar calendar = Calendar.getInstance();
				calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH) + 1;
				int day = calendar.get(Calendar.DAY_OF_MONTH);
				String dateStr = year + "" + (month < 10 ? ("0" + month) : month) + "" + (day < 10 ? ("0" + day) : day);
				if(valueFormat != null && valueFormat.contains("EEEE")){ // 需要拼接星期几的信息。
					dateStr += " " + Utils.getDayOfWeek(bv_, calendar);
				}
				setPropertyByName("value", dateStr);
			}
		}

		protected void onCreateSelectDialog(Activity activity) {

			LinearLayout ll = new LinearLayout(getContext());
			ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			ll.setGravity(Gravity.CENTER_VERTICAL);

			LPDateFieldView dateField = new LPDateFieldView(bv_, LPTextField.this);
			ll.addView(dateField);

			int popupAnimation_style = Utils.getResourcesId(activity, "popupAnimation", "style");
			final Dialog dlg = new Dialog(activity, popupAnimation_style);
			dlg.setContentView(ll);
			dlg.setCancelable(true);
			// 设置dialog位置
			Window mWindow = dlg.getWindow();
			mWindow.setWindowAnimations(popupAnimation_style);
			// 使对话框位于屏幕的底部并居中
			mWindow.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL);
			// 使对话框二边没有空隙
			mWindow.setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
			if (!activity.isFinishing())
				dlg.show();
			dateField.dialog_ = dlg;
		}

		public Component composited() {
			return LPTextField.this;
		}

		@Override
		protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
			super.onTextChanged(text, start, lengthBefore, lengthAfter);
			// �??��框内容发生变化时要相应更新其属性集合里的�? 若是日期控件则不需要下面操�?
			if (!isDateField_ && isWindowVisiable_) {
				setPropertyByName("value", String.valueOf(text) == null ? "" : String.valueOf(text));
			}
			String onchange = getPropertyByName("onchange");
			if (null != onchange && !"".equals(onchange)) {
				onClick(onchange);
			}
		}

		@Override
		protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
			if (focused) {
				String isKeyBoard = (String)getPropertyByName("isKeyBoard");
				if (isKeyBoard != null && isKeyBoard.equals("true")) {
					inputManager_.hideSoftInputFromWindow(this.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
				}
				
				String onFocus = getPropertyByName("onFocus");
				if (null != onFocus && !"".equals(onFocus)) {
					onClick(onFocus);
				}
			} else {
				String onBlur = getPropertyByName("onBlur");
				if (null != onBlur && !"".equals(onBlur)) {
					onClick(onBlur);
				}
			}

			super.onFocusChanged(focused, direction, previouslyFocusedRect);
		}

		@Override
		protected void onWindowVisibilityChanged(int visibility) {
			isWindowVisiable_ = (View.VISIBLE == visibility ? true : false);
			super.onWindowVisibilityChanged(visibility);
		}

		public void drawScanIcon() {
			addScanButton = true;
			BitmapDrawable bd = (BitmapDrawable) bv_.getResources().getDrawable(
					R.drawable.scan_icon);		
			this.setCompoundDrawablesWithIntrinsicBounds(null, null, bd, null);
			this.setHint(R.string.scan_hint);
			padLeft = bd.getIntrinsicWidth() + this.getPaddingLeft();
		}

		public void drawSearchIcon(){
			BitmapDrawable searchBitmap = (BitmapDrawable)bv_.getResources().getDrawable(R.drawable.searchjijin);
			this.setCompoundDrawablesWithIntrinsicBounds(null,null, searchBitmap, null);
			searchpadleft = searchBitmap.getIntrinsicWidth() + this.getPaddingLeft() ;
		}
	}
	/**
	 * 判断??��?�是密码控件，如果是则设置输入字符不明文显示
	 */
	public void isPassWord() {
		// TODO Auto-generated method stub
		String password = (String) getPropertyByName("type");
		if (null != password && password.equals("password")) {
			// 设置输入文字不明文显�?
			setTransformationMethod(new PasswordTransformationMethod());
		}
	}

	public String getContentText() {
		// TODO Auto-generated method stub
		MyLPTextField temp = (MyLPTextField) realView_;
		Object o = temp.getText();
		if (null != o)
			return o.toString();
		return "";
	}

	/**
	 * @param passwordTransformationMethod
	 */
	public void setTransformationMethod(PasswordTransformationMethod passwordTransformationMethod) {
		((MyLPTextField) realView_).setTransformationMethod(passwordTransformationMethod);

	}

	/*
	 * *************************************************************************
	 * Set and Get the attributes from XML documents.
	 * *************************************************************************
	 */
	/**
	 * @return
	 */
	public int constraint() {
		String sit = getAttribute(ATTRIBUTE_INPUT_CONSTRAINT);
		int constraint = -1;
		try {
			constraint = Integer.parseInt(sit);
		} catch (NumberFormatException e) {
			constraint = -1;
		}
		return constraint;
	}

	/**
	 * @return
	 */
	public int maxLeng() {
		int maxLength = 0;
		if (null != property_) {
			String maxSize = (String) property_.get(ATTRIBUTE_INPUT_MAXLENG);
			if (null != maxSize) {
				maxLength = Integer.parseInt(maxSize);
			}
		}
		return maxLength;
	}

	/**
	 * @param maxleng
	 */
	public void setMaxLeng(int maxleng) {
		String strMaxLeng = String.valueOf(maxleng);
	}

	/**
	 * @return
	 */
	public int minLeng() {
		String sminleng = (String) property_.get(ATTRIBUTE_INPUT_MINLENG);
		int minleng = -1;
		try {
			minleng = Integer.parseInt(sminleng);
		} catch (Exception e) {
			minleng = -1;
		}
		return minleng;
	}

	@Override
	public void initRealView(Activity activity, String value) {
		super.initRealView(activity, value);
		realView_ = new MyLPTextField(activity, value);
		// 设置5像素的左边距
		realView_.setPadding((int) Utils.getScaledValueX(5), 0, 0, 0);
		isPassWord();
	}

	public void setText(String value) {
		// TODO Auto-generated method stub
		MyLPTextField temp = (MyLPTextField) realView_;
		if (null == value)
			value = "";
		temp.setText(value);
	}

	/**
	 * 该方法主要用于指定控件的大小\显示颜色\样式�?如果服务端没有给定输出样式大�?则要自行计算一个合适的大小应用于该控件 Mould this component with its shape, color, size and
	 * so on.
	 * 
	 * @return
	 * @throws Exception
	 *             TODO
	 */
	public void mouldH() throws Exception {
		super.mouldH();
		if (null == realView_)
			return;
		MyLPTextField mlpl = (MyLPTextField) realView_;

		// 表示是否有边框，1表示有边框，0表示无边框，无边框的同时也无背景色(0/1)，什么都不写也表示有边框。
		String border = (String) getPropertyByName("border");
		if("".equals(border)){ // 什么都不写，默认有边框。
			BitmapDrawable bitdraw = new BitmapDrawable(makeBack("" , true));
			setBackgroundDrawable(bitdraw);
		}
		String align = getPropertyByName("align");
		if (null != cssStyle_) {
			if (null != cssStyle().content_
					&& cssStyle().content_.indexOf(CssStyle.BGIMAGEURL) < cssStyle().content_
					.indexOf(CssStyle.BACKGROUNDCOLOR)) {
				String temp = (String) cssStyle_.getProperty(CssStyle.BACKGROUNDCOLOR);
				if (null != temp && !temp.equals("")) {
					if("1".equals(border)){//有边框时 画背景色
						BitmapDrawable bitdraw = new BitmapDrawable(makeBack(temp , true));
						setBackgroundDrawable(bitdraw);
						realView_.setDrawingCacheBackgroundColor(Color.parseColor(temp));
					} else if("0".equals(border)){//border为0，不画边框。
						BitmapDrawable bitdraw = new BitmapDrawable(makeBack(temp , false));
						setBackgroundDrawable(bitdraw);
						realView_.setDrawingCacheBackgroundColor(Color.parseColor(temp));
					}
				}

			} else if (null != cssStyle().content_
					&& cssStyle().content_.indexOf(CssStyle.BGIMAGEURL) > cssStyle().content_
					.indexOf(CssStyle.BACKGROUNDCOLOR)) {
				setBackgroundDrawableByCssStyle();            
			}
			String alignTemp = cssStyle_.getProperty(CssStyle.TEXTALIGN);
			if (null != alignTemp && !alignTemp.equals(""))
				align = alignTemp;
		}
		if (align.equalsIgnoreCase("right")) {
			setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
		} else if (align.equalsIgnoreCase("center")) {
			setGravity(Gravity.CENTER);
		}
		// 设置输入框的提示语言
		mlpl.setHint((CharSequence) getPropertyByName("hold"));
	}

	/**
	 * border = 1 时画背景
	 * border = 0 或 "" 时，画边框(黑色边框，白色背景)
	 */
	private Bitmap makeBack(String bgColor , boolean isOnlyBorder) {
		Bitmap result = null;
		// 定义圆角
		float[] radii = new float[8];
		for (int i = 0; i < 8; i++) {
			radii[i] = 5;
		}
		// 建立一个和控件同样大小的bitmap。
		result = Bitmap.createBitmap(width_, height_, Bitmap.Config.ARGB_8888);
		// 建立一张画布。
		Canvas canvas = new Canvas(result); // 并且以后绘制都在这张画布上进行。
		// 设置画笔。
		Paint paint = new Paint();
		paint.setAntiAlias(true); // 画笔抗锯齿。
		// 定义一个画图的路径。
		Path path = new Path();

		if(!isOnlyBorder){
			paint.setColor(Color.parseColor(bgColor));
		} else { // 画背景。
			// 定义渲染样式
			LinearGradient bgLg = new LinearGradient(Utils.getScaledValueX(1), 
					Utils.getScaledValueY(3), Utils.getScaledValueX(1), 
					height_ - Utils.getScaledValueY(4),
					new int[] { 0xffededed, Color.WHITE }, null, TileMode.CLAMP);
			paint.setShader(bgLg);
			path.addRoundRect(new RectF(Utils.getScaledValueX(1),
					Utils.getScaledValueY(3), width_ - Utils.getScaledValueX(1),
					height_ - Utils.getScaledValueY(4)), radii,
					Path.Direction.CW); // 设置绘制路径。
					canvas.drawPath(path, paint);

					paint.setColor(Color.BLACK);
					paint.setStyle(Paint.Style.STROKE); // 设置空心。
					paint.setStrokeWidth(0.5f); // 设置画笔粗细。
		}
		// 画边线。
		// 定义渲染样式
		LinearGradient borderLg = new LinearGradient(Utils.getScaledValueX(1),
				Utils.getScaledValueY(3), Utils.getScaledValueX(1), 
				height_ - Utils.getScaledValueY(4),
				new int[] { Color.BLACK, 0xffb5b5b5 }, null, TileMode.CLAMP);
		paint.setShader(borderLg);
		path.addRoundRect(new RectF(Utils.getScaledValueX(1), 
				Utils.getScaledValueY(3), width_ - Utils.getScaledValueX(1),
				height_ - Utils.getScaledValueY(4)), radii,
				Path.Direction.CW); // 设置绘制路径。
				canvas.drawPath(path, paint); // 绘制边框。
				return result;
	}

	@Override
	public void releaseResource(Component component) {
		// TODO Auto-generated method stub
		super.releaseResource(component);
		Utils.releaseObject(addButton);
	}
}
